<template>
  <div class="app" id="app">
    <router-view></router-view>
  </div>
</template>

<script>
import loadJs from "@/common/loadJs";
import { init as clarityInit } from "./common/vue-clarity";
import FirebasePlugin from "@/common/firebase/firebase";
import { mapState } from "vuex";
import matchAndNavigate from "./common/router-map";
import { dottingFirstEntry } from '@/common/common';
export default {
  name: "app",
  data() {
    return {
      zeTime: "",
      debounceTimer: null,
    };
  },
  created() {
    let activityCode = this.getQueryString("activityCode");
    if (activityCode) {
      sessionStorage.setItem("ACTIVITY_CODE", activityCode);
    }

    let inviteCode = this.getQueryString("inviteCode");
    if (inviteCode) {
      sessionStorage.setItem("INVITE_CODE", inviteCode);
    }

    let event = this.getQueryString("event");
    if (event) {
      sessionStorage.setItem("eventValue", event);
    }

    this.getLocalStorageConfig();
    let token = localStorage.getItem("USER_TOKEN");
    if (token) {
      this.$store.dispatch("getUserInfo");
    }
    matchAndNavigate();
  },
  watch: {
    // '$route'(to) {
    //     this.$store.dispatch('getUserInfo').then((res)=>{
    //         this.$router.replace({path: this.$route.path, query: {inviteCode: res.inviteCode, ...to.query}})
    //     });
    // }
  },
  computed: {
    ...mapState({
      config: (state) => state.config.config,
    }),
  },
  mounted() {
    FirebasePlugin();
    this.$store.dispatch("getConfig").then(() => {
      // this.checkDeviceAndRedirect(); // 首次检查
    });
    // window.addEventListener('resize', this.debouncedCheckDeviceAndRedirect);
    if (!window.ssq) {
      loadJs(
        "https://assets.salesmartly.com/js/project_148531_154012_1729064107.js"
      ).then(() => {
        window.__ssc.setting = { hideIcon: true };
      });
    }

    if (!window.clarity) {
      clarityInit("no3wwxo3m2");
    }
		try{
			dottingFirstEntry();
		}catch(e){
			//TODO handle the exception
		}
  },
  beforeDestroy() {
    // window.removeEventListener('resize', this.debouncedCheckDeviceAndRedirect);
  },
  methods: {
    checkDeviceAndRedirect() {
      const width = window.innerWidth;
      const threshold = 768; // 设定的屏幕宽度阈值
      if (width < threshold) {
        // 如果屏幕宽度小于阈值且当前不在移动版路由下，则跳转到H5版页面
        window.open(this.config.h5Domain, "_self");
      }
    },
    debouncedCheckDeviceAndRedirect() {
      // 清除之前的定时器
      clearTimeout(this.debounceTimer);
      // 设置新的定时器
      this.debounceTimer = setTimeout(this.checkDeviceAndRedirect, 250);
    },
    getQueryString(name) {
      // 获取浏览器url参数
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return "";
    },
    getLocalStorageConfig() {
      // 获取本地选择的币种配置
      let currency = localStorage.getItem("user_currency");
      let language = localStorage.getItem("user_language");
      if (currency) {
        this.$store.commit("setUserCurrency", JSON.parse(currency));
      }

      if (language) {
        this.$store.commit("setUserLanguage", JSON.parse(language));
      }
    },
  },
};
</script>

<style lang="less" scoped>
.app {
  width: 100%;
}
</style>

<style lang="less">
::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 0;
}

::-webkit-scrollbar {
  -webkit-appearance: none;
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-thumb {
  cursor: pointer;
  border-radius: 3px;
  background: #c0c0c0;
  transition: color 0.2s ease;
}
</style>
